package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CustomLog;
import com.ruoyi.system.service.ICustomLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * custom日志Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@Controller
@RequestMapping("/system/log")
public class CustomLogController extends BaseController
{
    private String prefix = "system/log";

    @Autowired
    private ICustomLogService customLogService;

    @RequiresPermissions("system:log:view")
    @GetMapping()
    public String log()
    {
        return prefix + "/log";
    }

    @GetMapping("/customloglist")
    public String customloglist()
    {
        return prefix + "/customloglist";
    }

    /**
     * 查询custom日志列表
     */
    @RequiresPermissions("system:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustomLog customLog)
    {
        startPage();
        List<CustomLog> list = customLogService.selectCustomLogList(customLog);
        return getDataTable(list);
    }

    /**
     * 导出custom日志列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "custom日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustomLog customLog)
    {
        List<CustomLog> list = customLogService.selectCustomLogList(customLog);
        ExcelUtil<CustomLog> util = new ExcelUtil<CustomLog>(CustomLog.class);
        return util.exportExcel(list, "custom日志数据");
    }

    /**
     * 新增custom日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存custom日志
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "custom日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CustomLog customLog)
    {
        return toAjax(customLogService.insertCustomLog(customLog));
    }

    /**
     * 修改custom日志
     */
    @RequiresPermissions("system:log:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CustomLog customLog = customLogService.selectCustomLogById(id);
        mmap.put("customLog", customLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存custom日志
     */
    @RequiresPermissions("system:log:edit")
    @Log(title = "custom日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CustomLog customLog)
    {
        return toAjax(customLogService.updateCustomLog(customLog));
    }

    /**
     * 删除custom日志
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "custom日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(customLogService.deleteCustomLogByIds(ids));
    }

    @PostMapping("/customloglist")
    @ResponseBody
    public JsonResult<?> customloglist(@RequestParam("danweimingcheng")String ppDanweimingcheng, @RequestParam("jigoujibie")String ppJigoujibie,
                                            @RequestParam("diqu")String ppDiqu,
                                            @RequestParam("pageindex") int ppPageIndex,
                                            @RequestParam("pagesize") int ppPageSize)
    {
        try{
            List<Map<String,Object>> list = new ArrayList<>();
            int mmCount = 0;

            list = customLogService.findcustomlog(ppDanweimingcheng,ppJigoujibie,ppDiqu,ppPageIndex,ppPageSize);
            mmCount = customLogService.countcustomlog(ppDanweimingcheng,ppJigoujibie,ppDiqu);

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("loglist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            return JsonResult.getErrorResult("查询失败");
        }
    }
}
